//
//  CXBaseViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/28.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXBaseViewController.h"

#define kTitleButtonW       (DEVICE_SCREEN_WIDTH / self.childViewControllers.count)
#define kScrollIndicatorWidth   (30)

@interface CXBaseViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView * titleView; ///< 标题滚动视图
@property (nonatomic, weak) UIScrollView * contentScrollView;   ///< 内容滚动视图
@property (nonatomic, weak) UIButton * selectButton;    ///< 记录选中的标题按钮
@property (nonatomic, strong) NSMutableArray * titleButtons;    ///< 存储标题按钮
/** 记录是否初始化过标题view */
@property (nonatomic, assign) BOOL isInitializeTitleButtons;

/** 滚动条 */
@property (nonatomic, weak) UIView * scrollIndicatorView;

@end

@implementation CXBaseViewController

- (void)viewWillDisappear:(BOOL)animated {
    
}

- (void) viewDidDisappear:(BOOL)animated {
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.isInitializeTitleButtons = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 1. 添加标题滚动视图

    [self setupTitleScrollView];
    // 2. 添加内容滚动视图
    [self setupContentScrollView];

}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_isInitializeTitleButtons == NO) {
        // 4. 设置所有标题
        [self setupAllTitle];
        _isInitializeTitleButtons = YES;
    }
}

#pragma mark - 选中标题
- (void)selectButton:(UIButton *)btn {
    
    [_selectButton setTitleColor:[UIColor colorWithHexString:kBtnTitleColorNor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithHexString:kBtnTitleColorHigh] forState:UIControlStateNormal];
    
    _selectButton = btn;
}


#pragma mark - 添加一个子控制器的view
- (void)setupOneViewController:(NSInteger)i {
    
    NSInteger index = self.contentScrollView.contentOffset.x / self.contentScrollView.bounds.size.width;
    
    //取出自身控制器
    UIViewController * childVc = self.childViewControllers[index];
    //判断添加后就不用添加
    if (childVc.view.superview) {
        return;
    }
    childVc.view.frame = CGRectMake(index * self.contentScrollView.width, 0, self.contentScrollView.width, self.contentScrollView.height);
    [self.contentScrollView addSubview:childVc.view];

}



#pragma mark - 处理标题点击
- (void)didClickTitleButton:(UIButton *)btn {
    
    NSInteger i = btn.tag;
    
    // 1. 标题颜色 变成橙色
    [self selectButton:btn];
    
    // 2. 把对应子控制器的view添加上去
    [self setupOneViewController:i];
    
    // 3. 内容滚动到对应的位置
    CGFloat x = i * DEVICE_SCREEN_WIDTH;
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
}



#pragma mark - 设置所有标题
- (void)setupAllTitle {
    
    UIView * titleView = [[UIView alloc] init];
    self.titleView = titleView;
    titleView.backgroundColor = [UIColor whiteColor];
    titleView.frame = CGRectMake(0, 64 , self.view.width, 44);
    [self.view addSubview:titleView];
    
    // 添加所有标题按钮
    NSInteger count = self.childViewControllers.count;
    CGFloat btnW = titleView.width / count;
    CGFloat btnH = titleView.height;
    CGFloat btnX = 0;
    for (NSInteger i = 0; i < count; i++) {
        UIButton * titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        UIViewController * vc = self.childViewControllers[i];
        [titleButton setTitle:vc.title forState:UIControlStateNormal];
        titleButton.titleLabel.font = [UIFont fontWithName:FONTNAME_FZLTH size:14];
        btnX = i * btnW;
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
        [titleButton setTitleColor:[UIColor colorWithHexString:kBtnTitleColorNor] forState:UIControlStateNormal];
        
        // 监听按钮点击
        [titleButton addTarget:self action:@selector(didClickTitleButton:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            [self didClickTitleButton:titleButton];
        }
        
        [self.titleView addSubview:titleButton];
        
        [self.titleButtons addObject:titleButton];
    }
    
    // 设置内容的滚动范围
    self.contentScrollView.contentSize = CGSizeMake(count * DEVICE_SCREEN_WIDTH, 0);
    
    //设置滚动条
    UIView *scrollIndicatorView = [[UIView alloc] init];
    scrollIndicatorView.bounds = CGRectMake(0, 0, kScrollIndicatorWidth, 2);
    scrollIndicatorView.center = CGPointMake(kTitleButtonW / 2, self.titleView.height - 1);
    scrollIndicatorView.layer.cornerRadius = 1;
    scrollIndicatorView.backgroundColor = [UIColor colorWithHexString:kBtnTitleColorHigh];
    [self.titleView addSubview:scrollIndicatorView];
    _scrollIndicatorView = scrollIndicatorView;
}


#pragma mark - 添加标题滚动视图
- (void)setupTitleScrollView {
    // 创建titleView
    UIView * titleView = [[UIView alloc] init];
    titleView.frame = CGRectMake(0, 0, NAVIGATIONBAR_WIDTH, 44);
    [self.view addSubview:titleView];
    // 添加灰色分割线
    UILabel * lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, titleView.height - 0.5, titleView.width, 0.5)];
    lineLabel.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:lineLabel];
    
    _titleView = titleView;
}




#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView {
    // 创建contentScrollView
    UIScrollView * contentScrollView = [[UIScrollView alloc] init];
//    CGFloat y = CGRectGetMaxY(self.titleView.frame);
    contentScrollView.frame = CGRectMake(0, 108, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT - 108 - TABBAR_HEIGHT);
    // 设置contentScrollView的属性
    // 分页
    contentScrollView.pagingEnabled = YES;
    // 弹簧
    contentScrollView.bounces = NO;
    // 指示器
    contentScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理 - 目的：监听内容滚动视图 什么时候滚动完成
    contentScrollView.delegate = self;
    [self.view addSubview:contentScrollView];
    _contentScrollView = contentScrollView;
    
}


#pragma mark - UIScrollViewDelegate
// 滚动完成的时候调用
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSInteger i = btn.tag;
//    [self setupOneViewController:i];
//}

// 只要一滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    //获取当前角标
    NSInteger i = scrollView.contentOffset.x / DEVICE_SCREEN_WIDTH;
    //获取标题按钮
    UIButton *titleButton = self.titleButtons[i];
    
    CGFloat offsetX = scrollView.contentOffset.x / DEVICE_SCREEN_WIDTH - i;
    _scrollIndicatorView.center = CGPointMake(titleButton.center.x + kTitleButtonW * offsetX, self.titleView.height - 1);
    
    // 1. 标题颜色 变成橙色
    [self selectButton:titleButton];
    [self setupOneViewController:i];

}


#pragma mark - lazy load method
- (NSMutableArray *)titleButtons {
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}


@end
